package com.health.utils;

import java.util.List;
import java.util.Objects;

/**
 * 分页插件
 *
 * @param <T>
 */
public class PageInfo<T> {
    /**
     * 当前页码
     */
    private int currentPage = 1;

    /**
     * 当前页的记录数
     */
    private int pageSize = 5;

    /**
     * 总页数
     */
    private int totalPage;   // 得出总页数

    /**
     * 导航栏展示个数
     */
    private final int navigatePages = 5;

    /**
     * 导航栏当前第一个页码
     */
    private int navigateFirstPage;

    /**
     * 导航栏当前最后一个页码
     */
    private int navigateLastPage;

    /**
     * 总条数
     */
    private int totalCount;

    /**
     * 记录集合
     */
    private List<T> records;

    /**
     * 切换次数
     */
    private int navigatePageCircle;

    /**
     * 搜索参数值
     */
    private String searchValue;

    /**
     * 体检预约时间
     */
    private String searchExamineDate;


    public String getSearchExamineDate() {
        return searchExamineDate;
    }

    public void setSearchExamineDate(String searchExamineDate) {
        this.searchExamineDate = searchExamineDate;
    }

    public String getSearchValue() {
        return searchValue;
    }

    public void setSearchValue(String searchValue) {
        this.searchValue = searchValue;
    }

    public PageInfo() {
    }

    public PageInfo(int currentPage, int pageSize) {
        this.currentPage = currentPage;
        this.pageSize = pageSize;
    }

    // 传入 当前页码、当前页记录数、总页数、总条数、数据
    public PageInfo(int currentPage, int pageSize, int totalPage, int totalCount, List<T> records) {
        this.currentPage = currentPage;
        this.pageSize = pageSize;
        this.totalPage = totalPage;
        this.totalCount = totalCount;
        this.records = records;
    }

    public int getNavigatePages() {
        return navigatePages;
    }

    public int getNavigateFirstPage() {
        return navigateFirstPage;
    }

    public void setNavigateFirstPage(int navigateFirstPage) {
        this.navigateFirstPage = navigateFirstPage;
    }

    public int getNavigateLastPage() {
        return navigateLastPage;
    }

    public void setNavigateLastPage(int navigateLastPage) {
        this.navigateLastPage = navigateLastPage;
    }

    public int getNavigatePageCircle() {
        return navigatePageCircle;
    }

    public void setNavigatePageCircle(int navigatePageCircle) {
        this.navigatePageCircle = navigatePageCircle;
    }


    public int getCurrentPage() {
        return Objects.isNull(currentPage) ? 1 : this.currentPage;
    }

    public int getPageSize() {
        return Objects.isNull(pageSize) ? 10 : this.pageSize;
    }

    public int getTotalPage() {
        return this.getTotalCount() % this.getPageSize() == 0 ?
                this.getTotalCount() / this.getPageSize()
                : this.getTotalCount() / this.getPageSize() + 1;
    }

    public int getTotalCount() {
        return this.totalCount;
    }

    public List<T> getRecords() {
        return this.records;
    }

    public PageInfo<T> setCurrentPage(final int currentPage) {
        if (Objects.isNull(currentPage) || currentPage < 0) {
            this.currentPage = 1;
        } else {
            this.currentPage = currentPage;
        }
        return this;
    }

    public PageInfo<T> setPageSize(final int pageSize) {
        this.pageSize = pageSize;
        return this;
    }

    public void setTotalPage(final int totalPage) {
        this.totalPage = totalPage;
    }

    // 设置总记录数
    public void setTotalCount(final int totalCount) {
        this.totalCount = totalCount;
        this.setTotalPage(((this.totalCount - 1) / this.getPageSize()) + 1);
        this.setNavigatePageCircle((this.getCurrentPage() - 1) / navigatePages);  // 设置切换次数
        this.setNavigateFirstPage(this.getNavigatePageCircle() * navigatePages + 1); // 设置第一个页码
        this.setNavigateLastPage(Math.min(this.getNavigatePageCircle() * navigatePages + 5, this.getTotalPage()));
    }

    public void setRecords(final List<T> records) {
        this.records = records;
    }

    public boolean equals(final Object o) {
        if (o == this) {
            return true;
        } else if (!(o instanceof PageInfo)) {
            return false;
        } else {
            PageInfo<?> other = (PageInfo) o;
            if (!other.canEqual(this)) {
                return false;
            } else if (this.getCurrentPage() != other.getCurrentPage()) {
                return false;
            } else if (this.getPageSize() != other.getPageSize()) {
                return false;
            } else if (this.getTotalPage() != other.getTotalPage()) {
                return false;
            } else if (this.getTotalCount() != other.getTotalCount()) {
                return false;
            } else {
                Object this$records = this.getRecords();
                Object other$records = other.getRecords();
                if (this$records == null) {
                    if (other$records != null) {
                        return false;
                    }
                } else if (!this$records.equals(other$records)) {
                    return false;
                }

                return true;
            }
        }
    }

    protected boolean canEqual(final Object other) {
        return other instanceof PageInfo;
    }

    @Override
    public int hashCode() {
        int result = 1;
        long $currentPage = this.getCurrentPage();
        result = result * 59 + (int) ($currentPage >>> 32 ^ $currentPage);
        long $pageSize = this.getPageSize();
        result = result * 59 + (int) ($pageSize >>> 32 ^ $pageSize);
        long $totalPage = this.getTotalPage();
        result = result * 59 + (int) ($totalPage >>> 32 ^ $totalPage);
        long $totalCount = this.getTotalCount();
        result = result * 59 + (int) ($totalCount >>> 32 ^ $totalCount);
        Object $records = this.getRecords();
        result = result * 59 + ($records == null ? 43 : $records.hashCode());
        return result;
    }

    @Override
    public String toString() {
        return "PageInfo{" +
                "currentPage=" + currentPage +
                ", pageSize=" + pageSize +
                ", totalPage=" + totalPage +
                ", navigatePages=" + navigatePages +
                ", navigateFirstPage=" + navigateFirstPage +
                ", navigateLastPage=" + navigateLastPage +
                ", totalCount=" + totalCount +
                ", records=" + records +
                ", navigatePageCircle=" + navigatePageCircle +
                '}';
    }
}

